package com.jason.game.online.home.adapter


import android.widget.ImageView
import com.cby.library.common.ext.load
import com.cby.library.common.util.ScreenUtils
import com.cby.library.data.model.*
import com.cby.library.widget.GridLayout
import com.chad.library.adapter.base.BaseDelegateMultiAdapter
import com.chad.library.adapter.base.delegate.BaseMultiTypeDelegate
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.jason.game.online.R

/**
 * @description: HomeAdapter
 *
 * @author ChenBaoyang
 * @date 2021/10/27 16:45
 */
class HomeAdapter : BaseDelegateMultiAdapter<ModuleBean, BaseViewHolder>() {

    val gridOne = 1
    val gridTwo = 2
    val gridFour = 3
    val adOne = 4
    val adTwo = 5
    val none = 0

    init {

        // 设置代理
        setMultiTypeDelegate(object : BaseMultiTypeDelegate<ModuleBean>() {
            override fun getItemType(data: List<ModuleBean>, position: Int): Int {
                when (data[position].displayStyle) {
                    TYPE_GRID_1 -> return gridOne
                    TYPE_GRID_2 -> return gridTwo
                    TYPE_GRID_4 -> return gridFour
                    TYPE_AD_1 -> return adOne
                    TYPE_AD_2 -> return adTwo
                }
                return none
            }
        })
        // 绑定 item 类型
        getMultiTypeDelegate()?.let {
            it.addItemType(gridOne, R.layout.item_home_grid)
            it.addItemType(gridTwo, R.layout.item_home_grid)
            it.addItemType(gridFour, R.layout.item_home_grid)
            it.addItemType(adOne, R.layout.item_home_ad_1)
            it.addItemType(adTwo, R.layout.item_home_grid)
        }

    }


    override fun convert(holder: BaseViewHolder, item: ModuleBean) {
        when (holder.itemViewType) {
            gridOne -> {
                setTitleView(holder, item)
                val gridLayout = holder.getView<GridLayout>(R.id.grid_layout)
                //游戏排版 - 一列方式
                gridLayout.setNumColumns(1)
                val gameListItemAdapter = GameListItem1Adapter(context,
                    item.list as MutableList<GameInfoBean>
                )
                gridLayout.setAdapter(gameListItemAdapter)
            }
            gridTwo -> {
                setTitleView(holder, item)
                val gridLayout = holder.getView<GridLayout>(R.id.grid_layout)
                //游戏排版 - 一列方式
                gridLayout.setNumColumns(2)
                val gameListItemAdapter = GameGridItem2Adapter(context,
                    item.list as MutableList<GameInfoBean>
                )
                gridLayout.setAdapter(gameListItemAdapter)
            }
            gridFour -> {
                setTitleView(holder, item)
                val gridLayout = holder.getView<GridLayout>(R.id.grid_layout)
                //游戏排版 - 一列方式
                gridLayout.setNumColumns(4)
                gridLayout.horizontalSpace = ScreenUtils.dip2px(context, 13f)
                gridLayout.verticalSpace = ScreenUtils.dip2px(context, 14f)
                val gameListItemAdapter = GameGridItem4Adapter(context,
                    item.list as MutableList<GameInfoBean>
                )
                gridLayout.setAdapter(gameListItemAdapter)
            }
            adOne -> {
                item.list?.let {
                    if(it.isNotEmpty()) {
                        val imageView = holder.getView<ImageView>(R.id.iv_recommend_icon)
                        imageView.load((it[0] as RecAdBean).icon, R.drawable.loading_bixue)
                    }
                }
            }
            adTwo -> {
                item.list?.let {
                    //广告类型/专题 - 展示2列
                    val gridLayout = holder.getView<GridLayout>(R.id.grid_layout)
                    gridLayout.setNumColumns(2)
                    gridLayout.horizontalSpace = ScreenUtils.dip2px(context, 12f)
                    gridLayout.setAdapter(GameGridItemRecAd2Adapter(context, it as MutableList<RecAdBean>))
                }
            }
        }
    }

    private fun setTitleView(holder: BaseViewHolder, item: ModuleBean) {
        holder.setVisible(R.id.ll_header, item.showTitle == 1)
        if (item.showTitle == 1) {
            holder.setText(R.id.tv_title, item.title)
            holder.setVisible(R.id.tv_more, item.showMore == 1)
        }
    }



}